package com.cadre.common.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/* loaded from: DateUtil.class */
public class DateUtil {
    public static final String FORMAT_ONE = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_TWO = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_THREE = "yyyyMMdd-HHmmss";
    public static final String LONG_DATE_FORMAT = "yyyy-MM-dd";
    public static final String SHORT_DATE_FORMAT = "MM-dd";
    public static final String LONG_TIME_FORMAT = "HH:mm:ss";
    public static final String MONTG_DATE_FORMAT = "yyyy.MM";
    public static final String YEAR_DATE_FORMAT = "yyyy";
    public static final int SUB_YEAR = 1;
    public static final int SUB_MONTH = 2;
    public static final int SUB_DAY = 5;
    public static final int SUB_HOUR = 10;
    public static final int SUB_MINUTE = 12;
    public static final int SUB_SECOND = 13;
    protected static Log logger = LogFactory.getLog(DateUtil.class);
    static final String[] dayNames = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date stringtoDate(String dateStr, String format) {
        Date d;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(false);
            d = formater.parse(dateStr);
        } catch (Exception e) {
            d = null;
        }
        return d;
    }

    public static Date stringtoDate(String dateStr, String format, ParsePosition pos) {
        Date d;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(false);
            d = formater.parse(dateStr, pos);
        } catch (Exception e) {
            d = null;
        }
        return d;
    }

    public static String dateToString(Date date, String format) {
        String result = "";
        if (date == null) {
            return result;
        }
        try {
            result = new SimpleDateFormat(format).format(date);
        } catch (Exception e) {
        }
        return result;
    }

    public static String getCurrDate(String format) {
        return dateToString(new Date(), format);
    }

    public static String dateSub(int dateKind, String dateStr, int amount) {
        Date date = stringtoDate(dateStr, "yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(dateKind, amount);
        return dateToString(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
    }

    public static long timeSub(String firstTime, String secTime) {
        return (stringtoDate(secTime, "yyyy-MM-dd HH:mm:ss").getTime() - stringtoDate(firstTime, "yyyy-MM-dd HH:mm:ss").getTime()) / 1000;
    }

    public static int getDaysOfMonth(String year, String month) {
        int days;
        if (month.equals("1") || month.equals("3") || month.equals("5") || month.equals("7") || month.equals("8") || month.equals("10") || month.equals("12")) {
            days = 31;
        } else if (month.equals("4") || month.equals("6") || month.equals("9") || month.equals("11")) {
            days = 30;
        } else if ((Integer.parseInt(year) % 4 != 0 || Integer.parseInt(year) % 100 == 0) && Integer.parseInt(year) % 400 != 0) {
            days = 28;
        } else {
            days = 29;
        }
        return days;
    }

    public static int getDaysOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        return calendar.getActualMaximum(5);
    }

    public static int getToday() {
        return Calendar.getInstance().get(5);
    }

    public static int getToMonth() {
        return Calendar.getInstance().get(2) + 1;
    }

    public static int getToYear() {
        return Calendar.getInstance().get(1);
    }

    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(5);
    }

    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(1);
    }

    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(2) + 1;
    }

    public static long dayDiff(Date date1, Date date2) {
        return (date2.getTime() - date1.getTime()) / 86400000;
    }

    public static int yearDiff(String before, String after) {
        return getYear(stringtoDate(after, "yyyy-MM-dd")) - getYear(stringtoDate(before, "yyyy-MM-dd"));
    }

    public static int yearDiffCurr(String after) {
        return getYear(new Date()) - getYear(stringtoDate(after, "yyyy-MM-dd"));
    }

    public static long dayDiffCurr(String before) {
        return (stringtoDate(currDay(), "yyyy-MM-dd").getTime() - stringtoDate(before, "yyyy-MM-dd").getTime()) / 86400000;
    }

    public static int getFirstWeekdayOfMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(7);
        c.set(year, month - 1, 1);
        return c.get(7);
    }

    public static int getLastWeekdayOfMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(7);
        c.set(year, month - 1, getDaysOfMonth(year, month));
        return c.get(7);
    }

    public static String getNow() {
        return dateToString(Calendar.getInstance().getTime(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getAstro(String birth) {
        if (!isDate(birth)) {
            birth = "2000" + birth;
        }
        if (!isDate(birth)) {
            return "";
        }
        int month = Integer.parseInt(birth.substring(birth.indexOf("-") + 1, birth.lastIndexOf("-")));
        int start = (month * 2) - (Integer.parseInt(birth.substring(birth.lastIndexOf("-") + 1)) < new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22}[month - 1] ? 2 : 0);
        return "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substring(start, start + 2) + "座";
    }

    public static boolean isDate(String date) {
        StringBuffer reg = new StringBuffer("^((\\d{2}(([02468][048])|([13579][26]))-?((((0?");
        reg.append("[13578])|(1[02]))-?((0?[1-9])|([1-2][0-9])|(3[01])))");
        reg.append("|(((0?[469])|(11))-?((0?[1-9])|([1-2][0-9])|(30)))|");
        reg.append("(0?2-?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12");
        reg.append("35679])|([13579][01345789]))-?((((0?[13578])|(1[02]))");
        reg.append("-?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
        reg.append("-?((0?[1-9])|([1-2][0-9])|(30)))|(0?2-?((0?[");
        reg.append("1-9])|(1[0-9])|(2[0-8]))))))");
        return Pattern.compile(reg.toString()).matcher(date).matches();
    }

    public static Date nextMonth(Date date, int months) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.add(2, months);
        return cal.getTime();
    }

    public static Date nextDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.add(6, day);
        return cal.getTime();
    }

    public static String nextDay(int day, String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(6, day);
        return dateToString(cal.getTime(), format);
    }

    public static Date nextWeek(Date date, int week) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.add(4, week);
        return cal.getTime();
    }

    public static String currDay() {
        return dateToString(new Date(), "yyyy-MM-dd");
    }

    public static String befoDay() {
        return befoDay("yyyy-MM-dd");
    }

    public static String befoDay(String format) {
        return dateToString(nextDay(new Date(), -1), format);
    }

    public static String afterDay() {
        return dateToString(nextDay(new Date(), 1), "yyyy-MM-dd");
    }

    public static int getDayNum() {
        return (int) ((new GregorianCalendar().getTime().getTime() - new GregorianCalendar(1900, 1, 1).getTime().getTime()) / 86400000);
    }

    public static Date getDateByNum(int day) {
        return nextDay(new GregorianCalendar(1900, 1, 1).getTime(), day);
    }

    public static String getYmdDateCN(String datestr) {
        if (datestr == null || datestr.length() < 10) {
            return "";
        }
        StringBuffer buf = new StringBuffer();
        buf.append(datestr.substring(0, 4)).append(datestr.substring(5, 7)).append(datestr.substring(8, 10));
        return buf.toString();
    }

    public static String getFirstDayOfMonth(String format) {
        Calendar cal = Calendar.getInstance();
        cal.set(5, 1);
        return dateToString(cal.getTime(), format);
    }

    public static String getLastDayOfMonth(String format) {
        Calendar cal = Calendar.getInstance();
        cal.set(5, 1);
        cal.add(2, 1);
        cal.add(5, -1);
        return dateToString(cal.getTime(), format);
    }

    public static String dateToStr(Date date) {
        String dateStr;
        SimpleDateFormat sdf = new SimpleDateFormat(MONTG_DATE_FORMAT);
        if (date != null) {
            dateStr = sdf.format(date);
        } else {
            dateStr = sdf.format(new Date());
        }
        return dateStr;
    }

    public static Date strToDate(String str) {
        return stringtoDate(str, MONTG_DATE_FORMAT);
    }

    public static String dateToYear(Date date) {
        String dateStr;
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_DATE_FORMAT);
        if (date != null) {
            dateStr = sdf.format(date);
        } else {
            dateStr = sdf.format(new Date());
        }
        return dateStr;
    }

    public static String subMonth(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(MONTG_DATE_FORMAT);
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(2, 1);
        return sdf.format(rightNow.getTime());
    }

    public static void main(String[] args) {
        System.out.println(dateToYear(null));
        System.out.println(stringtoDate("2019.01", MONTG_DATE_FORMAT));
        Map<String, Object> map = new HashMap<>();
        map.put("20190429", 20190429);
        map.put("20190430", 20190430);
        map.put("20190506", 20190506);
        map.put("20190507", 20190507);
        map.put("20190508", 20190508);
        map.put("20190509", 20190509);
        map.put("20190510", 20190510);
        map.put("20190511", 20190511);
        map.put("20190513", 20190513);
        map.put("20190514", 20190514);
        map.put("20190515", 20190515);
        map.put("20190516", 20190516);
        map.put("20190517", 20190517);
        map.put("20190520", 20190520);
        map.put("20190521", 20190521);
        map.put("20190522", 20190522);
        map.put("20190523", 20190523);
        map.put("20190524", 20190524);
        map.put("20190525", 20190525);
        map.put("20190527", 20190527);
        map.put("20190528", 20190528);
        map.put("20190529", 20190529);
        map.put("20190530", 20190530);
        map.put("20190531", 20190531);
        map.put("20190604", 20190604);
        map.put("20190605", 20190605);
        map.put("20190606", 20190606);
        map.put("20190610", 20190610);
        map.put("20190611", 20190611);
        map.put("20190612", 20190612);
        map.put("20190613", 20190613);
        map.put("20190614", 20190614);
        map.put("20190615", 20190615);
        System.out.println("%%%%%%%%%%%%%%%%%%%" + map.size() + "%%%%%%%%%%%%%%%%%%%%%%");
    }
}
